﻿// --------------------------------
// <copyright file="Player.cs" company="Faculty of Information Technology, Czech Technical University in Prague">
//     This document is shared by Microsoft Public License (MS-PL)
//     For further details please see http://msdn.microsoft.com/en-us/library/ff648068.aspx or license.txt.
// </copyright>
// <author>Lukáš Molnár</author>

// <email>molnaluk@fit.cvut.cz</email>

// <date>9. 11. 2011 22:22:36</date>

// <summary>
//
// </summary>
// --------------------------------

using System;

namespace FITClub.Episode1.Controller
{
    /// <summary>
    ///
    /// </summary>
    public abstract class APlayer : IDisposable
    {
        protected VirtualGamepadState last;
        protected VirtualGamepadState actual;

        public string Name { get; private set; }

        public VirtualGamepadState ControllerStateActual
        {
            get { return actual; }
            protected set { this.actual = value; }
        }

        public VirtualGamepadState ControllerStatePrevious { get { return last; } }

        public APlayer(string name)
        {
            this.Name = name;
            this.actual = VirtualGamepadState.Default;
            this.last = VirtualGamepadState.Default;
        }

        /// <summary>
        /// Handles changes in game state.
        /// </summary>
        public virtual void Update()
        {
            this.last = this.ControllerStateActual;
            this.actual = VirtualGamepadState.Default;
        }

        public abstract void Dispose();

        protected void ControllerAction(int button, Action<string> isDownAction, Action<string> releasedAction)
        {
            if (this.ControllerStateActual[button])
            {
                isDownAction(this.Name);
            }
            else
            {
                if (this.ControllerStatePrevious[button])
                {
                    releasedAction(this.Name);
                }
            }
        }

        protected void SetState(bool state, int button)
        {
            this.actual[button] = state;
        }
    }
}